from django.utils.translation import gettext_lazy as _
from models.choices import *
from django.db import models


class VipPackage(models.Model):
    """
    VIP套餐
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    name = models.CharField(max_length=255, default="", verbose_name=_("名称"))
    day = models.IntegerField(default=0, verbose_name=_("天数"))
    price = models.FloatField(default=0, verbose_name=_("价格"))
    order = models.IntegerField(default=1, verbose_name=_("排序"))
    set_time = models.DateTimeField(auto_now=True, auto_now_add=False, verbose_name=_("设置时间"))
    brief_introduction = models.CharField(max_length=255, default="", verbose_name=_("简介"))
    equity_statement = models.TextField(default="", verbose_name=_("权益说明"))

    class Meta:
        db_table = "vip_package"
        verbose_name = _("VIP套餐")
        db_table_comment = _("VIP套餐")
        verbose_name_plural = _("VIP套餐")


class AgentChannel(models.Model):
    """
    代理商渠道
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    name = models.CharField(default="", max_length=255, verbose_name=_("渠道名称"))

    class Meta:
        db_table = "agent_channel"
        verbose_name = _("代理商渠道")
        db_table_comment = _("代理商渠道")
        verbose_name_plural = _("代理商渠道")


class AgentPackage(models.Model):
    """
    代理商套餐
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    name = models.CharField(max_length=255, default="", verbose_name=_("名称"))
    channel = models.ManyToManyField(AgentChannel, verbose_name=_("渠道"), through="AgentChannelShip",
                                     through_fields=("package", "channel"))
    water_card_number = models.IntegerField(default=0, verbose_name=_("水卡数量"))
    price = models.FloatField(default=0, verbose_name=_("价格"))
    username = models.CharField(max_length=255, default="", null=True, verbose_name=_("联系人"))
    phone_number = models.CharField(max_length=255, default="", null=True, verbose_name=_("联系方式"))
    address = models.CharField(max_length=255, default="", null=True, verbose_name=_("联系地址"))
    set_time = models.DateTimeField(auto_now=True, auto_now_add=False, verbose_name=_("设置时间"))

    class Meta:
        db_table = "agent_package"
        verbose_name = _("代理商套餐")
        db_table_comment = _("代理商套餐")
        verbose_name_plural = _("代理商套餐")


class AgentChannelShip(models.Model):
    """
    代理商渠道配送
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    channel = models.ForeignKey(AgentChannel, on_delete=models.CASCADE, verbose_name=_("渠道"))
    package = models.ForeignKey(AgentPackage, on_delete=models.CASCADE, verbose_name=_("套餐"))

    class Meta:
        db_table = "agent_channel_ship"
        verbose_name = _("代理商渠道配送")


class User(models.Model):
    """
    用户信息表
    """
    id = models.AutoField(primary_key=True, verbose_name=_('id'))
    openid = models.CharField(verbose_name=_('openid'), max_length=64, null=False, default='', unique=True)
    union_id = models.CharField(verbose_name=_('union_id'), max_length=64, null=False, default='', db_index=True)
    nickname = models.CharField(verbose_name=_('昵称'), max_length=64, null=True, default='')
    avatar = models.CharField(verbose_name=_('头像'), max_length=255, null=True, default='')
    session_key = models.CharField(verbose_name='session_key', max_length=255, default="")
    gender = models.SmallIntegerField(verbose_name=_('性别'), help_text=_('性别'), choices=Gender.choices,
                                      null=True, blank=True, default=Gender.UNKNOWN)
    language = models.CharField(max_length=50, default="", verbose_name=_("语言"))
    country = models.CharField(max_length=50, default="", verbose_name=_("国家"))
    province = models.CharField(max_length=50, default="", verbose_name=_("省份"))
    city = models.CharField(max_length=50, default="", verbose_name=_("城市"))
    phone_number = models.CharField(max_length=11, default="", verbose_name=_("电话号码"))
    create_time = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name=_("注册时间"))
    invite_id = models.IntegerField(verbose_name=_('邀请人id'), null=True, blank=True)
    invite_code = models.CharField(verbose_name=_('邀请码'), max_length=64, null=False, default='', db_index=True)
    water_card_number = models.IntegerField(verbose_name=_('水卡数量'), null=True, default=0, blank=True)
    water_droplets_number = models.IntegerField(verbose_name=_('水滴数量'), default=0, null=True, blank=True)
    user_type = models.ForeignKey(VipPackage, on_delete=models.CASCADE, verbose_name=_('用户身份'), null=True)
    member_expiration_time = models.DateTimeField(verbose_name=_('会员到期时间'), null=True, blank=True)
    agent_type = models.ForeignKey(AgentPackage, on_delete=models.CASCADE, verbose_name=_('用户身份'), null=True)
    in_come = models.FloatField(verbose_name=_('收益'), default=0, null=True, blank=True)
    date_of_birth = models.DateField(verbose_name=_('出生日期'), help_text=_('出生日期'), null=True, blank=True)

    class Meta:
        db_table = "user"
        verbose_name_plural = _("用户信息")
        verbose_name = _("用户信息")
        db_table_comment = _("用户信息")


class VipShip(models.Model):
    """
    会员身份设置
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_("用户"))
    vip = models.ForeignKey(VipPackage, on_delete=models.CASCADE, verbose_name=_("会员身份"))
    set_time = models.DateTimeField(auto_now=True, auto_now_add=False, verbose_name=_("设置时间"))
    note = models.CharField(max_length=255, default="", verbose_name=_("备注"))

    class Meta:
        db_table = "vip_ship"
        verbose_name = _("会员身份设置")
        db_table_comment = _("会员身份设置")
        verbose_name_plural = _("会员身份设置")


class AgentShip(models.Model):
    """
    代理商身份设置
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_("用户"))
    agent = models.ForeignKey(AgentPackage, on_delete=models.CASCADE, verbose_name=_("代理商身份"))
    province = models.CharField(max_length=255, default="", verbose_name=_("省"))
    city = models.CharField(max_length=255, default="", verbose_name=_("市"))
    district = models.CharField(max_length=255, default="", verbose_name=_("区"))
    set_time = models.DateTimeField(auto_now=True, auto_now_add=False, verbose_name=_("设置时间"))
    note = models.CharField(max_length=255, default="", verbose_name=_("备注"))

    class Meta:
        db_table = "agent_ship"
        verbose_name = _("代理商身份设置")
        db_table_comment = _("代理商身份设置")
        verbose_name_plural = _("代理商身份设置")


class Advertisement(models.Model):
    """
    首页广告信息
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    picture = models.CharField(max_length=255, verbose_name=_("图片url"))
    link = models.CharField(max_length=255, default="", verbose_name=_("链接"))
    type = models.SmallIntegerField(default=IndexAdType.APPLETS_INDEX_AD, choices=IndexAdType.choices,
                                    verbose_name=_("类型"))

    class Meta:
        db_table = "advertising"
        verbose_name = _("广告信息")
        db_table_comment = _("广告信息")
        verbose_name_plural = _("用户信息")


class GoodType(models.Model):
    """
    商品类型
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    name = models.CharField(max_length=255, default="", verbose_name=_("名称"))

    class Meta:
        db_table = "good_type"
        verbose_name = _("商品类型")
        db_table_comment = _("商品类型")
        verbose_name_plural = _("商品类型")


class Goods(models.Model):
    """
    商品信息
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    name = models.CharField(max_length=255, default="", verbose_name=_("名称"))
    picture = models.CharField(max_length=255, default="", verbose_name=_("图片url"))
    describe = models.TextField(default="", verbose_name=_("描述"))
    status = models.SmallIntegerField(default=GoodStatus.SALE, choices=GoodStatus.choices, verbose_name=_("状态"))
    type = models.ForeignKey(GoodType, on_delete=models.CASCADE, verbose_name=_("类型"))
    browsing_number = models.IntegerField(default=0, verbose_name=_("点击次数"))
    sales_number = models.IntegerField(default=0, verbose_name=_("销量"))
    buyers_number = models.IntegerField(default=0, verbose_name=_("购买人数"))
    collections_number = models.IntegerField(default=0, verbose_name=_("收藏人数"))
    shopping_cart_number = models.IntegerField(default=0, verbose_name=_("购物车人数"))
    list_order = models.IntegerField(default=0, verbose_name=_("排序"))
    operation_time = models.DateTimeField(auto_now=True, auto_now_add=False, verbose_name=_("操作时间"))
    shipping_address = models.CharField(max_length=255, default="", verbose_name=_("发货地址"))

    class Meta:
        db_table = "good"
        verbose_name = _("商品信息")
        db_table_comment = _("商品信息")
        verbose_name_plural = _("用户信息")


class GoodSpecification(models.Model):
    """
    商品规则
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    title = models.CharField(max_length=255, default="", verbose_name=_("标题"))
    good = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name=_("商品"), related_name="specs")
    spec = models.CharField(max_length=255, default="", verbose_name=_("规格"))
    price = models.FloatField(default=0, verbose_name=_("价格"))
    vip_price = models.FloatField(default=0, verbose_name=_("会员价"))
    normal_agent_price = models.FloatField(default=0, verbose_name=_("普通代理价"))
    regional_agent_price = models.FloatField(default=0, verbose_name=_("地区代理价"))
    province_agent_price = models.FloatField(default=0, verbose_name=_("省级代理价"))
    freight = models.FloatField(default=0, verbose_name=_("运费"))
    water_droplets = models.IntegerField(default=0, verbose_name=_("水滴数"))
    water_cards = models.IntegerField(default=0, verbose_name=_("水卡数"))
    province_agency_income = models.FloatField(default=0, verbose_name=_("省级代理收入"))
    regional_income = models.FloatField(default=0, verbose_name=_("地区收入"))
    inventory = models.IntegerField(default=0, verbose_name=_("库存"))

    class Meta:
        db_table = "good_specification"
        verbose_name = _("商品规则")
        db_table_comment = _("商品规则")
        verbose_name_plural = _("商品规则")


class GoodPicture(models.Model):
    """
    商品图片
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    good = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name=_("商品"))
    picture = models.CharField(max_length=255, default="", verbose_name=_("图片url"))

    class Meta:
        db_table = "good_picture"
        verbose_name = _("商品图片")
        db_table_comment = _("商品图片")
        verbose_name_plural = _("商品图片")


class WaterCardRecord(models.Model):
    """
    水卡信息
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    name = models.CharField(max_length=255, default="", verbose_name=_("任务名称"))
    card_type = models.SmallIntegerField(default=WaterCardType.GET, choices=WaterCardType.choices,
                                         verbose_name=_("类型"))
    number = models.IntegerField(default=0, verbose_name=_("数量"))
    time = models.DateTimeField(auto_now=True, auto_now_add=False, verbose_name=_("时间"))
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_("用户"))

    class Meta:
        db_table = "water_card"
        verbose_name_plural = _("水卡信息")
        db_table_comment = _("水卡信息")
        verbose_name = _("水卡信息")


class WaterCardTransferRecord(models.Model):
    """
    水卡划拨记录
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    card_type = models.SmallIntegerField(default=WaterCardType.GET, choices=WaterCardType.choices,
                                         verbose_name=_("类型"))
    op_user = models.ForeignKey(User, on_delete=models.SET_NULL, related_name="op_user", null=True,
                                verbose_name=_("划拨人"))
    receive_user = models.ForeignKey(User, on_delete=models.SET_NULL, related_name="receive_user", null=True,
                                     verbose_name=_("接收人"))
    number = models.IntegerField(default=0, verbose_name=_("数量"))
    time = models.DateTimeField(auto_now=True, auto_now_add=False, verbose_name=_("时间"))

    class Meta:
        db_table = "water_card_transfer"
        verbose_name = _("水卡划拨记录")
        db_table_comment = _("水卡划拨记录")
        verbose_name_plural = _("水卡划拨记录")


class WaterDropletsTask(models.Model):
    """
    水滴获取类型
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    task_name = models.CharField(max_length=255, default="", verbose_name=_("任务名称"))
    droplet_number = models.IntegerField(default=0, verbose_name=_("数量"))
    set_time = models.DateTimeField(auto_now=True, auto_now_add=False, verbose_name=_("设置时间"))
    on = models.BooleanField(default=False, verbose_name=_("是否开启"))

    class Meta:
        db_table = "water_droplets_task"
        verbose_name = _("水滴获取类型")
        db_table_comment = _("水滴获取类型")
        verbose_name_plural = _("水滴获取类型")


class WaterDropletsRecord(models.Model):
    """
    水滴记录
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    user = models.ForeignKey("User", on_delete=models.SET_NULL, null=True, verbose_name=_("user id"))
    task = models.ForeignKey("WaterDropletsTask", on_delete=models.SET_NULL, null=True, verbose_name=_("任务"))
    droplet_number = models.IntegerField(default=0, verbose_name=_("数量"))
    op_type = models.SmallIntegerField(default=WaterCardType.GET, choices=WaterCardType.choices, verbose_name=_("类型"))
    op_time = models.DateTimeField(auto_now=True, auto_now_add=False, verbose_name=_("操作时间"))

    class Meta:
        db_table = "water_droplets_record"
        verbose_name = _("水滴记录")
        db_table_comment = _("水滴记录")
        verbose_name_plural = _("水滴记录")


class OrderInfo(models.Model):
    """
    订单
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    user = models.ForeignKey("User", on_delete=models.SET_NULL, null=True, verbose_name=_("用户"))
    create_time = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name=_("下单时间"))
    transaction_time = models.DateTimeField(verbose_name=_("交易时间"), null=True)
    transaction_status = models.SmallIntegerField(default=TransactionStatus.PENDING_PAYMENT,
                                                  choices=TransactionStatus.choices,
                                                  verbose_name=_("交易状态"))
    delivery_status = models.SmallIntegerField(default=DeliveryStatus.TO_BE_SHIPPED, choices=DeliveryStatus.choices,
                                               verbose_name=_("配送状态"))
    after_sales_status = models.SmallIntegerField(default=AfterSalesStatus.NO_REFUND,
                                                  choices=AfterSalesStatus.choices, verbose_name=_("售后状态"))
    refund_reason = models.CharField(max_length=34, default="", verbose_name=_("退款原因"))
    refund_time = models.DateTimeField(verbose_name=_("退款时间"), null=True)
    order_id = models.CharField(max_length=255, default="", verbose_name=_("订单号"))
    transaction_id = models.CharField(max_length=255, default="", null=True, verbose_name=_("微信订单号"))
    logistics_id = models.CharField(max_length=255, default="", verbose_name=_("物流单号"))
    Logistics_company = models.CharField(max_length=255, default="", verbose_name=_("物流公司"))
    good = models.ForeignKey("Goods", on_delete=models.SET_NULL, null=True, verbose_name=_("商品"))
    good_spec = models.ForeignKey("GoodSpecification", on_delete=models.SET_NULL, null=True, verbose_name=_("商品规格"))
    good_number = models.IntegerField(default=0, verbose_name=_("商品数量"))
    address = models.ForeignKey("Address", on_delete=models.SET_NULL, null=True, verbose_name=_("地址"))
    consume_water_card = models.FloatField(default=0, verbose_name=_("消费水卡"))
    consume_water_droplets = models.FloatField(default=0, verbose_name=_("消费水滴"))
    actual_payment = models.FloatField(default=0, verbose_name=_("实际支付"))
    pay_type = models.SmallIntegerField(default=PayType.WECHAT, choices=PayType.choices, verbose_name=_("支付方式"))

    class Meta:
        db_table = "order_info"
        verbose_name = _("订单信息")
        db_table_comment = _("订单信息")


class VipOrderInfo(models.Model):
    """
    VIP订单
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    order_id = models.CharField(max_length=255, default="", verbose_name=_("订单号"))
    transaction_id = models.CharField(max_length=255, default="", null=True, verbose_name=_("微信订单号"))
    user = models.ForeignKey("User", on_delete=models.SET_NULL, null=True, verbose_name=_("user id"))
    create_time = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name=_("创建时间"))
    transaction_time = models.DateTimeField(verbose_name=_("交易时间"), null=True)
    transaction_status = models.SmallIntegerField(default=TransactionStatus.PENDING_PAYMENT,
                                                  choices=TransactionStatus.choices,
                                                  verbose_name=_("交易状态"))
    after_sales_status = models.SmallIntegerField(default=AfterSalesStatus.NO_REFUND,
                                                  choices=AfterSalesStatus.choices, verbose_name=_("售后状态"))
    refund_reason = models.CharField(max_length=34, default="", verbose_name=_("退款原因"))
    refund_time = models.DateTimeField(verbose_name=_("退款时间"), null=True)
    pay_type = models.SmallIntegerField(default=PayType.WECHAT, choices=PayType.choices, verbose_name=_("支付方式"))
    package = models.ForeignKey("VipPackage", on_delete=models.SET_NULL, null=True, verbose_name=_("套餐"))
    pay_money = models.FloatField(default=0, verbose_name=_("支付金额"))
    note = models.CharField(max_length=255, default="", verbose_name=_("备注"))

    class Meta:
        db_table = "vip_order_info"
        verbose_name = _("VIP订单")
        db_table_comment = _("VIP订单")
        verbose_name_plural = _("VIP订单")


class AgentOrderInfo(models.Model):
    """
    代理商订单
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    order_id = models.CharField(max_length=255, default="", verbose_name=_("订单号"))
    transaction_id = models.CharField(max_length=255, default="", null=True, verbose_name=_("微信订单号"))
    operator = models.ForeignKey("User", on_delete=models.SET_NULL, related_name="operator", null=True,
                                 verbose_name=_("操作人"))
    package = models.ForeignKey("AgentPackage", on_delete=models.SET_NULL, null=True, verbose_name=_("套餐"))
    province = models.CharField(max_length=255, default="", verbose_name=_("省"))
    city = models.CharField(max_length=255, default="", verbose_name=_("市"))
    district = models.CharField(max_length=255, default="", verbose_name=_("区"))
    create_time = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name=_("创建时间"))
    transaction_time = models.DateTimeField(auto_now_add=False, null=True, verbose_name=_("交易时间"))
    pay_money = models.FloatField(default=0, verbose_name=_("支付金额"))
    exec_user = models.ForeignKey("User", on_delete=models.SET_NULL, related_name="exec_user", null=True,
                                  verbose_name=_("被操作人"))
    water_card_number = models.IntegerField(default=0, verbose_name=_("水卡数量"))
    op_type = models.SmallIntegerField(default=AgentOperationType.WECHAT, choices=AgentOperationType.choices,
                                       verbose_name=_("操作类型"))

    class Meta:
        db_table = "agent_order_info"
        verbose_name = _("代理商订单")
        db_table_comment = _("代理商订单")
        verbose_name_plural = _("代理商订单")


class RegionalRevenue(models.Model):
    """
    区域收益
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    region_address = models.CharField(max_length=255, default="", verbose_name=_("区域地址"))
    revenue_total = models.FloatField(default=0, verbose_name=_("总收益"))
    sale_water_card = models.FloatField(default=0, verbose_name=_("卖出水卡数量"))

    class Meta:
        db_table = "regional_revenue"
        verbose_name = _("区域收益")
        db_table_comment = _("区域收益")
        verbose_name_plural = _("区域收益")


class ShopCart(models.Model):
    """
    购物车
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    user = models.ForeignKey("User", on_delete=models.SET_NULL, null=True, verbose_name=_("user id"))
    good = models.ForeignKey("Goods", on_delete=models.SET_NULL, null=True, verbose_name=_("good id"))
    good_number = models.IntegerField(default=0, verbose_name=_("good number"))
    spec = models.ForeignKey("GoodSpecification", on_delete=models.SET_NULL, null=True, verbose_name=_("spec id"))

    class Meta:
        db_table = "shop_cart"
        verbose_name = _("购物车")
        db_table_comment = _("购物车")


class BrowsingRecord(models.Model):
    """
    浏览记录
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    user = models.ForeignKey("User", on_delete=models.SET_NULL, null=True, verbose_name=_("用户"))
    good = models.ForeignKey("Goods", on_delete=models.SET_NULL, null=True, verbose_name=_("商品"))
    create_time = models.DateTimeField(auto_now=True, auto_now_add=False, verbose_name=_("创建时间"))

    class Meta:
        db_table = "browsing_record"
        verbose_name = _("浏览记录")
        db_table_comment = _("浏览记录")


class SearchRecord(models.Model):
    """
    搜索记录
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    user = models.ForeignKey("User", on_delete=models.SET_NULL, null=True, verbose_name=_("user id"))
    keyword = models.CharField(max_length=255, default="", verbose_name=_("关键字"))

    class Meta:
        db_table = "search_record"
        verbose_name = _("搜索记录")
        db_table_comment = _("搜索记录")


class Address(models.Model):
    """
    收货地址
    """
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey("User", on_delete=models.SET_NULL, null=True, verbose_name=_("user_id"))
    consignee = models.CharField(max_length=255, db_comment="收货人", verbose_name=_("收货人"))
    phone_number = models.CharField(max_length=11, default="", db_comment="手机号", verbose_name=_("手机号"))
    province_code = models.CharField(
        max_length=12, blank=True, null=True, db_comment="省级区划编号"
    )
    province_name = models.CharField(
        max_length=32, blank=True, null=True, db_comment="省级名称"
    )
    city_code = models.CharField(
        max_length=12, blank=True, null=True, db_comment="市级区划编号"
    )
    city_name = models.CharField(
        max_length=32, blank=True, null=True, db_comment="市级名称"
    )
    district_code = models.CharField(
        max_length=12, blank=True, null=True, db_comment="区级区划编号"
    )
    district_name = models.CharField(
        max_length=32, blank=True, null=True, db_comment="区级名称"
    )
    detail = models.CharField(
        max_length=200, blank=True, null=True, db_comment="详细地址", verbose_name="详细地址"
    )
    label = models.CharField(
        max_length=100, blank=True, null=True, db_comment="标签", verbose_name="标签"
    )
    is_default = models.IntegerField(
        db_comment="默认 0 否 1是", default=Whether.YES, choices=Whether.choices, verbose_name="是否默认"
    )
    create_time = models.DateTimeField(db_comment="创建时间", auto_now_add=True, auto_now=False)
    update_time = models.DateTimeField(
        db_comment="更新时间", auto_now_add=False, auto_now=True, verbose_name="更新时间"
    )

    class Meta:
        db_table = "address"
        verbose_name = _("收货地址")
        db_table_comment = _("收货地址")


class RegionInfo(models.Model):
    """
    邀请记录
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    parent_id = models.IntegerField(default=0, verbose_name=_("父级id"))
    name = models.CharField(max_length=255, default="", verbose_name=_("名称"))

    class Meta:
        db_table = "region_info"
        verbose_name = _("region info")
        db_table_comment = _("区域信息")
        verbose_name_plural = _("区域信息")


class CustomerServiceRecords(models.Model):
    """
    客服记录
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    question_user = models.ForeignKey("User", on_delete=models.SET_NULL, null=True, verbose_name=_("提问用户"))
    customer_number = models.IntegerField(default=0, verbose_name=_("客服编号"))
    question = models.TextField(default="", verbose_name=_("咨询内容"))
    answer = models.TextField(default="", verbose_name=_("回答内容"))
    question_time = models.DateTimeField(db_comment="提问时间", auto_now_add=True, auto_now=False)
    answer_time = models.DateTimeField(db_comment="回答时间", null=True)

    class Meta:
        db_table = "customer_service_records"
        verbose_name = _("客服记录")
        db_table_comment = _("客服记录")


class CustomerServiceFeedbackRecord(models.Model):
    """
    客服反馈记录
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    feedback_user = models.ForeignKey("User", on_delete=models.SET_NULL, null=True, verbose_name=_("提问用户"))
    customer_number = models.IntegerField(default=0, verbose_name=_("客服编号"))
    feedback_content = models.TextField(default="", verbose_name=_("反馈内容"))
    score = models.IntegerField(default=0, verbose_name=_("评分"))
    create_time = models.DateTimeField(db_comment="反馈时间", auto_now_add=True, auto_now=False)

    class Meta:
        db_table = "customer_service_feedback_record"
        verbose_name = _("客服反馈记录")
        db_table_comment = _("客服反馈记录")


class CommonQuestion(models.Model):
    """
    常见问题
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    question = models.TextField(default="", verbose_name=_("问题"))
    answer = models.TextField(default="", verbose_name=_("回答"))
    create_time = models.DateTimeField(db_comment="创建时间", auto_now_add=True, auto_now=False)

    class Meta:
        db_table = "common_question"
        verbose_name = _("常见问题")
        db_table_comment = _("常见问题")
        verbose_name_plural = _("常见问题")


class KnowledgeBase(models.Model):
    """
    知识库
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    file = models.CharField(max_length=255, default="", verbose_name=_("文件"))
    create_time = models.DateTimeField(db_comment="创建时间", auto_now_add=True, auto_now=False)

    class Meta:
        db_table = "knowledge_base"
        verbose_name = _("知识库")
        db_table_comment = _("知识库")


class CustomerManage(models.Model):
    """
    客服管理
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    user = models.ForeignKey("ManagerUser", on_delete=models.SET_NULL, null=True, verbose_name=_("user id"))
    customer_number = models.CharField(default="", max_length=255, verbose_name=_("客服编号"))
    status = models.SmallIntegerField(default=Whether.YES, choices=Whether.choices, verbose_name=_("状态"))
    create_time = models.DateTimeField(db_comment="创建时间", auto_now_add=True, auto_now=False)
    update_time = models.DateTimeField(db_comment="更新时间", auto_now_add=False, auto_now=True)

    class Meta:
        db_table = "customer_manage"
        verbose_name = _("客服管理")
        db_table_comment = _("客服管理")


class SignRecord(models.Model):
    """
    签到记录
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    user = models.ForeignKey("User", on_delete=models.SET_NULL, null=True, verbose_name=_("user id"))
    key = models.CharField(max_length=255, default="", verbose_name=_("key"))
    sign_content = models.TextField(default="", verbose_name=_("签到内容"))

    class Meta:
        db_table = "sign_record"
        verbose_name = _("签到记录")
        db_table_comment = _("签到记录")


class Menu(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=_('menu id'))
    name = models.CharField(max_length=255, default='', verbose_name=_('menu name'))
    icon = models.CharField(max_length=255, default='', verbose_name=_('css icon'))
    link = models.CharField(max_length=255, default='', verbose_name=_('link'))
    father = models.ForeignKey("self", on_delete=models.CASCADE, null=True, related_name='father_set',
                               verbose_name=_('super menu id'))
    apis = models.ManyToManyField('Api')

    class Meta:
        db_table = 'menu'
        verbose_name = _('menu')


class Api(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=_('api id'))
    method = models.IntegerField(default=0, choices=HttpMethod.choices, null=True,
                                 verbose_name=_('api http request method'))
    name = models.CharField(max_length=255, default='', verbose_name=_('api name'))
    position = models.CharField(max_length=128, default='', verbose_name=_('api position'))
    group = models.CharField(max_length=128, default='', verbose_name=_('api group'))
    user_default = models.BooleanField(default=0, verbose_name=_('is user default'))

    class Meta:
        db_table = 'api'
        verbose_name = _('api')
        verbose_name_plural = _('api')


class PermissionGroup(models.Model):
    """
    权限组
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    description = models.CharField(max_length=255, default="", verbose_name=_("描述"))
    name = models.CharField(max_length=255, default="", verbose_name=_("名称"))
    type = models.SmallIntegerField(default=0, choices=PermissionGroupType.choices)
    menu = models.ManyToManyField('Menu')
    api = models.ManyToManyField('Api')

    class Meta:
        db_table = "permission_group"
        verbose_name = _("权限组")
        db_table_comment = _("权限组")


class ManagerUser(models.Model):
    """
    管理员用户
    """
    id = models.AutoField(primary_key=True, verbose_name=_("id"))
    username = models.CharField(max_length=255, default="", verbose_name=_("用户名"))
    password = models.CharField(max_length=255, default="", verbose_name=_("密码"))
    alias = models.CharField(max_length=255, default="", verbose_name=_("别名"))
    email = models.CharField(max_length=255, default="", verbose_name=_("邮箱"))
    mobile = models.CharField(max_length=255, default="", verbose_name=_("手机号"))
    is_active = models.IntegerField(
        db_comment="是否激活 0 否 1是", default=Whether.YES, choices=Whether.choices, verbose_name="是否激活"
    )
    is_super = models.IntegerField(
        db_comment="是否超级管理员 0 否 1是", default=Whether.NO, choices=Whether.choices, verbose_name="是否超级管理员"
    )
    create_time = models.DateTimeField(db_comment="创建时间", auto_now_add=True, auto_now=False)
    creator = models.IntegerField(null=True, verbose_name=_('creator'))
    permission_group = models.ForeignKey('PermissionGroup', null=True, on_delete=models.SET_NULL)

    _is_authenticated = False

    @property
    def is_authenticated(self):
        return self._is_authenticated

    def authenticate(self):
        self._is_authenticated = True

    class Meta:
        db_table = "manager_user"
        verbose_name = _("管理员用户")
        db_table_comment = _("管理员用户")
